# API一致性检查脚本
# 检查前后端接口调用是否一致

Write-Host "=== API一致性检查 ===" -ForegroundColor Green

# 定义接口规范（基于微信小程序API路由清单文档）
$apiSpecs = @{
    "auth_login" = @{
        "path" = "/api/miniprogram/auth/login"
        "method" = "POST"
        "frontend_call" = "auth/login"
    }
    "auth_profile" = @{
        "path" = "/api/miniprogram/auth/profile"
        "method" = "GET"
        "frontend_call" = "auth/profile"
    }
    "auth_bind" = @{
        "path" = "/api/miniprogram/auth/bind"
        "method" = "POST"
        "frontend_call" = "auth/bind"
    }
    "health" = @{
        "path" = "/api/miniprogram/health"
        "method" = "GET"
        "frontend_call" = "health"
    }
}

Write-Host "`n检查后端路由定义..." -ForegroundColor Yellow

# 检查后端路由
$backendFiles = @(
    "backend/api/miniprogram/auth_api.py",
    "backend/api/miniprogram/__init__.py"
)

foreach ($file in $backendFiles) {
    if (Test-Path $file) {
        Write-Host "检查文件: $file" -ForegroundColor Cyan
        $content = Get-Content $file -Raw
        
        foreach ($api in $apiSpecs.Keys) {
            $spec = $apiSpecs[$api]
            $routePattern = "@miniprogram_bp\.route\('$($spec.path.Replace('/api/miniprogram', ''))'"
            
            if ($content -match $routePattern) {
                Write-Host "  ✅ $($spec.path) - 已定义" -ForegroundColor Green
            } else {
                Write-Host "  ❌ $($spec.path) - 未找到" -ForegroundColor Red
            }
        }
    }
}

Write-Host "`n检查前端调用..." -ForegroundColor Yellow

# 检查前端调用
$frontendFiles = @(
    "miniprogram/utils/auth.js",
    "miniprogram/pages/index/index.js"
)

foreach ($file in $frontendFiles) {
    if (Test-Path $file) {
        Write-Host "检查文件: $file" -ForegroundColor Cyan
        $content = Get-Content $file -Raw
        
        foreach ($api in $apiSpecs.Keys) {
            $spec = $apiSpecs[$api]
            $callPattern = $spec.frontend_call
            
            if ($content -match $callPattern) {
                Write-Host "  ✅ $callPattern - 已调用" -ForegroundColor Green
            } else {
                Write-Host "  ❌ $callPattern - 未找到" -ForegroundColor Red
            }
        }
    }
}

Write-Host "`n=== 检查完成 ===" -ForegroundColor Green
Write-Host "`n建议:" -ForegroundColor Yellow
Write-Host "1. 确保所有接口都在API规范文档中定义" -ForegroundColor White
Write-Host "2. 前后端接口路径必须完全一致" -ForegroundColor White
Write-Host "3. 新增接口前先更新规范文档" -ForegroundColor White 